<?php
/**
 * (ajax)
 */
namespace App\Admin\Ajax\Shipping;

use App\Jobs\Shipping\SkuApproval;
use App\Models\Product\ProductSku;
use App\Models\Shipping\SSku;
use App\Models\Shipping\SSkuApply;
use App\Models\Shipping\SSkuTotal;
use App\Models\Shipping\SSkuTotalApply;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
class TotalAjax {

    /**
     * 一键锁定
     * @return \Illuminate\Http\JsonResponse
     */
    public function totalLock(){
        try{
            //提货汇总
            $items = SSkuTotal::query()->where('is_lock',0)->get(); //未锁定
            $i = 0;
            $num = 100;
            while(true){
                if($items->isEmpty())break;
                DB::beginTransaction();
                $item = $items->splice($i,$num);

                foreach ($item as $st){
                    $st->is_lock = 1; //已锁定
                    $flag1 = $st->save();
                    if($flag1){
                        //提货sku
                        SSku::query()
                            ->where('psku_id',$st->psku_id)
                            ->update([
                                'is_lock' => 1 //已锁定
                            ]);
                    }
                }
                DB::commit();
            }
            return response()->json(['status' => true, 'message' => '操作成功！']);
        }catch (\Exception $e){
            DB::rollBack();
            report($e);
            return response()->json(['status' => false, 'message' => '系统异常！']);
        }
    }

    /**
     * 一键解锁
     * @return \Illuminate\Http\JsonResponse
     */
    public function totalUnLock(){
        try{
            //提货汇总
            $items = SSkuTotal::query()->where('is_lock',1)->get(); //已锁定
            $i = 0;
            $num = 100;
            while(true){
                if($items->isEmpty())break;
                DB::beginTransaction();
                $item = $items->splice($i,$num);
                foreach ($item as $st){
                    $st->is_lock = 0; //未锁定
                    $flag1 = $st->save();
                    if($flag1){
                        //提货sku
                        SSku::query()
                            ->where('psku_id',$st->psku_id)
                            ->update([
                                'is_lock' => 0 //未锁定
                            ]);
                    }
                }
                DB::commit();
            }
            return response()->json(['status' => true, 'message' => '操作成功！']);
        }catch (\Exception $e){
            DB::rollBack();
            report($e);
            return response()->json(['status' => false, 'message' => '系统异常！']);
        }
    }

    public function totalSync(){
        try{
            Artisan::call('shipping:sync-total');
//            $day = date('j');
//            $date = $day <= 15 ? date('Y-m-01') : date('Y-m-16');
//            SSkuTotal::query()
//                ->orderBy('id')
//                ->chunk(200, function ($items) use ($date){
//                    DB::beginTransaction();
//                    foreach ($items as $item){
//                        $apply = SSkuTotalApply::query()
//                            ->where('shipping_date','>=',$date)
//                            ->where('psku_id',$item->psku_id)
//                            ->get();
//                        foreach ($apply as $_apply){
//                            $approvalNum = SSkuApply::query()
//                                ->where('psku_id',$item->psku_id)
//                                ->where('shipping_date',$_apply->shipping_date)
//                                ->sum('approval_num');
//                            $_apply->apply_num = $approvalNum;
//                            $_apply->save();
//                        }
//                    }
//                    DB::commit();
//                });
            return response()->json(['status' => true, 'message' => '操作成功！']);
        }catch (\Exception $e){
            report($e);
            return response()->json(['status' => false, 'message' => '系统异常！']);
        }
    }

    /**
     * 同步单个sku的提货汇总数量
     * @return \Illuminate\Http\JsonResponse
     */
    public function totalSyncSku(){
        try{
            $sku = trim(request('sku')[0]);
            if(is_null($sku) || empty($sku)) throw new \Exception('sku编码不能为空!');

            $productSku = ProductSku::getModelByCode($sku);
            if(!$productSku) throw new \Exception('系统未匹配到当前sku产品!');

            //$skuIdArr = SSkuApply::query()
            //->where('shipping_date','>=',date('Y-m-01'))
            //->groupBy('psku_id')
            //->pluck('psku_id')
            //->toArray();

            //同步单个sku的提货汇总
            $shippingDate = SSkuApply::initShippingDate();
            SkuApproval::dispatch([$productSku->id],$shippingDate);

            return response()->json(['status' => true, 'message' => '操作成功！']);
        }catch (\Exception $e){
            report($e);
            return response()->json(['status' => false, 'message' => $e->getMessage()]);
        }
    }

}
